Page({
  data: {
    arr: [],
    swiperArr: [],
    pageNum: 1,
    searchKey: "" 
  },
  goDetail(evt) {
    let proid = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/detail?proid=${proid}`, 
    });
  },
  onLoad(options) {
    console.log('页面加载完毕');
    this.loadPageData(function () {
      console.log('请求成功了');
    });
    this.loadSwiperData();
  },
  loadSwiperData() {
    wx.request({
      url: 'http://47.94.6.186:3001/api/banner/list',
      method: 'GET',
      success: res => {
        console.log(res.data);
        this.setData({
          swiperArr: res.data.data
        });
      }
    });
  },
  loadPageData(callback) {
    let url = 'http://47.94.6.186:3001/api/pro/list';
    let data = {
      count: this.data.pageNum,
      limitNum: 10
    };
    // 如果有搜索关键词，拼接搜索参数
    if (this.data.searchKey.trim()!== "") {
      url = `http://47.94.6.186:3001/api/pro/search?keyword=${this.data.searchKey}`; 
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      data: data,
      success: res => {
        console.log(res.data);
        this.setData({
          arr: this.data.arr.concat(res.data.data)
        });
        if (callback) {
          callback();
        }
      },
      fail: err => {
        console.log(err);
      }
    });
  },
  // 新增搜索输入事件处理函数
  onSearchInput(evt) {
    this.setData({
      searchKey: evt.detail.value
    });
  },
  // 新增搜索确认事件处理函数
  onSearchConfirm() {
    this.data.arr = []; 
    this.data.pageNum = 1; 
    this.loadPageData(function () {
      console.log('搜索请求成功了');
    });
  },
  onReady() {
    console.log('onReady');
  },
  onShow() {
    console.log('页面展示--onShow');
  },
  onHide() {
    console.log('页面隐藏或者进入后台');
  },
  onUnload() {
    console.log('页面被卸载了');
  },
  onPullDownRefresh() {
    console.log('监听用户下拉动作');
    this.data.arr = [];
    this.data.pageNum = 1;
    this.loadPageData(function () {
      console.log('下拉刷新请求成功了-结束了下拉刷新');
      wx.stopPullDownRefresh();
    });
  },
  onReachBottom() {
    console.log('滚动到底部');
    this.data.pageNum++;
    console.log(this.data.pageNum);
    this.loadPageData();
  },
  onShareAppMessage() {}
});